/*
 * Created by SharpDevelop.
 * User: phtrung
 * Date: 6/23/2008
 * Time: 1:38 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace QuickResizeImage
{
	class MainClass
	{
		public static void Main(string[] args)
		{
			//0 source
			//1 destination
			//2 new size ( WxH ; Wx100 ; 100xH; 100x100)
			string src="", des="", att="";
			
			
			if(args.Length >= 2 ){
				foreach(string tmp in args){
					if(tmp.ToUpper().IndexOf("/S")== 0 ) src = tmp.Replace("/S ","");
					if(tmp.ToUpper().IndexOf("/D")== 0 ) des = tmp.Replace("/D ","");
					if(tmp.ToUpper().IndexOf("/A")== 0 ) att = tmp.Replace("/A ","");
				}
				if(src.Length == 0 || att.Length == 0 ){ 
					Console.WriteLine("Not enouth parameters!");
					Console.Read();
				}else if(!File.Exists(src)){
					Console.WriteLine("The source file is not existed!");
					Console.Read();
				}else{
					reSize(src, des, att);
				}
				
			}else{
				Console.WriteLine("EXE /S [/D] /A" );
				Console.WriteLine("/S: source file (ex: /S c:\\demo.png)" );
				Console.WriteLine("/D: Destination file(save same file if missing) (ex: /D c:\\demo.png)" );
				Console.WriteLine("/A: New size of image (ex: /A WxH or Wx100 )" );
				Console.WriteLine("Ex: EXE /S C:\\demo.png /D C:\\abc.png /A 300x200");
				Console.Read();
			}
		}
		
		public static void reSize( string src, string des, string size ){						
			Image current = Image.FromFile(src);
			
			int width  = ( (size.ToUpper().IndexOf("W")>=0) ? current.Width  : Convert.ToInt16(size.ToUpper().Split('X')[0]) );
			int height = ( (size.ToUpper().IndexOf("H")>=0) ? current.Height : Convert.ToInt16(size.ToUpper().Split('X')[1]) );			
			
			Image result = createImage(width,height);
			Graphics painter = Graphics.FromImage(result);
			painter.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
			painter.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
			painter.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
			painter.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
			painter.DrawImage(current, 0, 0, width, height);
			painter.Save();
			
			ImageFormat format = ImageFormat.Png;
			if(des.IndexOf(".gif") >=0 ) format = ImageFormat.Gif;
			if(des.IndexOf(".jpg") >=0 ) format = ImageFormat.Jpeg;
			
			current.Dispose();
			
			if(des.Length == 0) des = src;
			
			result.Save(des, format);
		}
		
		public static string[] getFileList(string path, string extension){
			return Directory.GetFiles( path, extension );
		}
		
		public static Image createImage( int nWidth, int nHeight )
		{
		  Bitmap result = new Bitmap( nWidth, nHeight );		 
		  return (Image)result;
		}
		
		public static Image cropImage( Image img, int x, int y, int nWidth, int nHeight )
		{
		  Bitmap result = new Bitmap( nWidth, nHeight );		  
		  
		  using( Graphics g = Graphics.FromImage( (Image) result ) )
		    g.DrawImageUnscaled( img, x, y, nWidth, nHeight );
		  return (Image)result;
		}
	}
}
